home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK1.toast / Development Kits (Disc 1) / Apple Shared Library Manager / ASLM Examples / Sample Apps / CSample / Sources / SampleLibrary.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-19  |  7.6 KB  |  262 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        SampleLibrary.c
  3.  
  4.     Contains:    Implementation of the CSampleLibrary shared library.
  5.  
  6.     Copyright:    © 1993-1994 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10.  
  11. #include <LibraryManager.h>
  12. #include <LibraryManagerUtilities.h>
  13.  
  14. #include <String.h>
  15. #include <Events.h>
  16. #include <QuickDraw.h>
  17. #include <Resources.h>
  18. #include <Menus.h>
  19. #include <Windows.h>
  20. #include <StdIO.h>
  21.  
  22. #include "SampleLibrary.h"            /* our traffic light library */
  23.  
  24. /* GLOBALS */
  25.  
  26. /* The "g" prefix is used to emphasize that a variable is global. */
  27.  
  28. Boolean     gLightState = false;    /* state of the traffic light */
  29. WindowPtr    gWindow;                /* global traffic light window */
  30. Rect        gStopRect;                /* rectangle for stop light, set up by NewTrafficLight */
  31. Rect        gGoRect;                /* rectangle for go light, set up by NewTrafficLight */
  32.  
  33. /* STATIC FUNCTIONS */
  34.  
  35. static Boolean GoGetRect( short rectID, Rect *theRect);
  36.  
  37. /*————————————————————————————————————————————————————————————————————————————————————
  38.     GoGetRect
  39.     
  40.     loads the global rectrangles that are used for drawing traffic lights.
  41. ————————————————————————————————————————————————————————————————————————————————————*/
  42.     
  43. static Boolean GoGetRect( short rectID, Rect *theRect)
  44. {
  45.     Handle        resource;
  46.     
  47.     resource = GetResource('RECT', rectID);    /* get 'RECT' resource from library file */
  48.     if ( resource != nil ) {
  49.         *theRect = **((Rect**) resource);
  50.         return true;
  51.     }
  52.     else
  53.         return false;
  54. }
  55.  
  56. /*————————————————————————————————————————————————————————————————————————————————————
  57.     NewTrafficLight
  58.     
  59.     allocate trafficlight window and initilize the globals.
  60. ————————————————————————————————————————————————————————————————————————————————————*/
  61.  
  62. OSErr    NewTrafficLight()
  63. {
  64.     long            savedrefnum;
  65.     OSErr            err;
  66.     MenuHandle        themenu;
  67.     TLibraryFile    *libraryfile;
  68.     
  69.     Trace("NewTrafficLight\n");            /* send the output to trace monitor's window */
  70.     
  71.     gLightState = false;
  72.  
  73.     /* include the library's file in resource chain so we can allocate our menu
  74.        and window resources */
  75.  
  76.     libraryfile = GetLocalLibraryFile();
  77.  
  78.     if( err = Preflight( libraryfile, &savedrefnum ) ) {
  79.         Trace("NewTrafficLight failed to Preflight Error = %d\n", err );
  80.         return err;
  81.     }
  82.  
  83.     gWindow = GetNewWindow( rWindow, (Ptr) nil, (WindowPtr) -1 );
  84.  
  85.     GoGetRect(rStopRect, &gStopRect);    /* get rectangle for Stop light */
  86.     GoGetRect(rGoRect, &gGoRect);        /* get rectangle for Go light */
  87.  
  88.     /* if there is a MENU resources in our shared library we should append it 
  89.        to our application */
  90.  
  91.     themenu = GetMenu( mLight );        /* get our traffic light menu */
  92.     InsertMenu( themenu, 0 );            /* append to the application's menu */
  93.     
  94.     DrawMenuBar();                        /* go ahead and update the menu bar */
  95.     
  96.     /* now we are done with accessing our library resource; restore the chain
  97.        to it previous state */
  98.  
  99.     if( err = Postflight( libraryfile, savedrefnum ) )
  100.         Trace("NewTrafficLight failed to Postflight error = %d\n", err );
  101.     
  102.     return err;
  103. }
  104.  
  105. /*————————————————————————————————————————————————————————————————————————————————————
  106.     FreeTrafficLight
  107.     
  108.     deallocate the memory used by trafficlight
  109. ————————————————————————————————————————————————————————————————————————————————————*/
  110.  
  111. void FreeTrafficLight()
  112. {
  113.     Trace("FreeTrafficLight\n");        /* tell Trace Monitor where we are */
  114.     DisposeWindow(gWindow);                /* application get ride of the window */
  115. }
  116.  
  117. /*————————————————————————————————————————————————————————————————————————————————————
  118.     GetLightState
  119.     
  120.     return current state of traffic light
  121. ————————————————————————————————————————————————————————————————————————————————————*/
  122.  
  123. Boolean GetLightState()
  124. {
  125.     Trace("GetLightState\n");                /* tell Trace Monitor where we are */
  126.     return gLightState    ;                /* return the state of Traffic Light */
  127. }
  128.  
  129. /*————————————————————————————————————————————————————————————————————————————————————
  130.     SetLightState
  131.     
  132.     set the light to the new state
  133. ————————————————————————————————————————————————————————————————————————————————————*/
  134.  
  135. void SetLightState( Boolean newState )
  136. {
  137.     Trace("SetLightState\n");                /* tell Trace Monitor what we are doing */
  138.     gLightState = newState;                /* set the new state */
  139.     
  140.     SetPort(gWindow);                    /* make sure we are at the right port before */
  141.     InvalRect(&gWindow->portRect);        /* invalidating the update region */
  142. }
  143.  
  144. /*————————————————————————————————————————————————————————————————————————————————————
  145.     DrawLight
  146.     
  147.     draw the actual traffic light
  148. ————————————————————————————————————————————————————————————————————————————————————*/
  149.  
  150. void DrawLight()
  151. {
  152.     GrafPtr        oldport;
  153.     
  154.     Trace("DrawLight\n");                /* tell the Trace Monitor what we are doing */
  155.  
  156.     if( gWindow ) {
  157.     
  158.         GetPort( &oldport );            /* save the old port */
  159.         SetPort( gWindow );                /* set the new port to our object's port */
  160.                 
  161.         EraseRect(&gWindow->portRect);    /* clear out any garbage that may linger */
  162.         if ( gLightState )                /* draw a red (or white) stop light */
  163.             ForeColor(redColor);
  164.         else
  165.             ForeColor(whiteColor);
  166.         
  167.         PenSize( 2, 2 );                
  168.         PaintOval(&gStopRect);            
  169.         ForeColor(blackColor);
  170.         FrameOval(&gStopRect);
  171.         if ( ! gLightState )            /* draw a green (or white) go light */
  172.             ForeColor(greenColor);
  173.         else
  174.             ForeColor(whiteColor);
  175.         PaintOval(&gGoRect);
  176.         ForeColor(blackColor);
  177.         FrameOval(&gGoRect);
  178.         PenSize( 1, 1 );
  179.  
  180.         SetPort( oldport );                /* restore the grafpointer */
  181.     }
  182. }
  183.  
  184. /*————————————————————————————————————————————————————————————————————————————————————
  185.     AdjustTrafficLightMenus
  186.     
  187.     Enable and disable traffic light's menus based on the current state.
  188. ————————————————————————————————————————————————————————————————————————————————————*/
  189.  
  190. void AdjustTrafficLightMenus( Boolean active )
  191. {
  192.     long                savedrefnum;
  193.     MenuHandle            menu;
  194.     TLibraryFile        *libraryfile;
  195.     OSErr                 err;
  196.  
  197.     Trace("AdjustTrafficLightMenus\n");    /* tell the Trace Monitor where we are */
  198.     
  199.     libraryfile = GetLocalLibraryFile();
  200.  
  201.     /* include the library in resource chain so we can allocate our menu resources */
  202.     
  203.     if( err = Preflight( libraryfile, &savedrefnum ) ) {
  204.         Trace("AdjustMenus failed to Preflight Error = %d\n", err );
  205.         return;
  206.     }
  207.     
  208.     if(  menu = GetMenuHandle(mLight) ) {    /* get the handle to traffic light menu */
  209.         
  210.         if ( active ) {                    /* do we need to enable them? */
  211.             EnableItem(menu, iStop);
  212.             EnableItem(menu, iGo);
  213.         } else {
  214.             DisableItem(menu, iStop);
  215.             DisableItem(menu, iGo);
  216.         }
  217.  
  218.         CheckItem(menu, iStop, gLightState); /* we can also determine check/uncheck state, too */
  219.         CheckItem(menu, iGo, ! gLightState);        
  220.     }
  221.     else
  222.         Trace("GetMenuHandle returned NIL\n");    /* let Trace Monitor know we failed */
  223.  
  224.                                         /* restore the chain to its previous state */
  225.     if( err = Postflight( libraryfile, savedrefnum ) )
  226.         Trace("AdjustMenus failed to Postflight error = %d\n", err );
  227. }
  228.  
  229.  
  230. /*————————————————————————————————————————————————————————————————————————————————————
  231.     DoTrafficLightMenuCommand
  232.     
  233.     Enable and disable menus based on the current state of traffic light.
  234. ————————————————————————————————————————————————————————————————————————————————————*/
  235.  
  236. void DoTrafficLightMenuCommand( short menuItem )
  237. {
  238.     long                savedrefnum;
  239.     TLibraryFile        *libraryfile;
  240.     OSErr                 err;
  241.  
  242.     Trace("DoTrafficLightMenuCommand\n");/* tell Trace Monitor what we are doing */
  243.                                         /* include the library's file in resource chain */
  244.     libraryfile = GetLocalLibraryFile();
  245.     if( err = Preflight( libraryfile, &savedrefnum ) ) {
  246.         Trace("AdjustMenus failed to Preflight Error = %d\n", err );
  247.         return;
  248.     }
  249.  
  250.     switch ( menuItem ) {                /* what menu item selected? */
  251.         case iStop:
  252.             SetLightState( true );            /* set and update */
  253.             break;
  254.         case iGo:
  255.             SetLightState( false );            /* set and  update */
  256.             break;
  257.     }
  258.                                         /* restore the chain to its previous state */
  259.     if( err = Postflight( libraryfile, savedrefnum ) )
  260.         Trace("AdjustMenus failed to Postflight error = %d\n", err );
  261. }
  262.